#include "CFife.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "CGame.h"
#include "CXInput.h"
#include "CEmitter.h"
#include "CPlayer.h"

CFife::CFife()
{
	m_nPipeSound[0] = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/cdown.wav");
	m_nPipeSound[1] = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/cright.wav");
	m_nPipeSound[2] = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/cleft.wav");
	m_nPipeSound[3] = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/cup.wav");
	m_fCounter = 0;
}

CFife::~CFife()
{

}

CFife* CFife::GetInstance()
{
	static CFife instance;
	return &instance;
}

bool CFife::Input()
{
	///////////////////////////////////////////////////////////////////////
	//Xbox 360 Controller Input
	if(CXInput::GetInstance()->IsConnected())
	{
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_X)
		{
			//cdown
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[0]))
			{
				//CPlayer::GetInstance()->GetBlue()->PlayEffect(true);
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[0]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[0], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('0');
				return true;
			}
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A)
		{
			//cright
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[1]))
			{
				//CPlayer::GetInstance()->GetGreen()->PlayEffect(true);
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[1]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[1], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('1');
				return true;
			}
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_B)
		{
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[2]))
			{
				//CPlayer::GetInstance()->GetRed()->PlayEffect(true);
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[2]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[2], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('2');
				return true;
			}
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_Y)
		{
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[3]))
			{
				//CPlayer::GetInstance()->GetYellow()->PlayEffect(true);
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[3]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[3], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('3');
				return true;
			}
		}
	}
	///////////////////////////////////////////////////////////////////////

	///////////////////////////////////////////////////////////////////////
	//Keyboard Input
	else
	{
		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_1))
		{
			//cdown
			//CPlayer::GetInstance()->GetBlue()->PlayEffect(true);
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[0]))
			{
				
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[0]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[0], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('0');
				return true;
			}
		}
		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_2))
		{
			//cright
			//CPlayer::GetInstance()->GetGreen()->PlayEffect(true);
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[1]))
			{
				
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[1]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[1], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('1');
				return true;
			}
		}
		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_3))
		{
			//CPlayer::GetInstance()->GetRed()->PlayEffect(true);
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[2]))
			{
				
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[2]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[2], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('2');
				return true;
			}
		}
		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_4))
		{
			//CPlayer::GetInstance()->GetYellow()->PlayEffect(true);
			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nPipeSound[3]))
			{
				
				CSGD_FModManager::GetInstance()->PlaySound(m_nPipeSound[3]);
				CSGD_FModManager::GetInstance()->SetVolume(m_nPipeSound[3], CGame::GetInstance()->SFXVol());
				m_szNoteStack.push_back('3');
				return true;
			}
		}
	}
	return false;
	////////////////////////////////////////////////////////////////////
	/*if(CPlayer::GetInstance()->GetBlue()->GetPlay() == true)
		CPlayer::GetInstance()->GetBlue()->PlayEffect(false);
	if(CPlayer::GetInstance()->GetGreen()->GetPlay() == true)
		CPlayer::GetInstance()->GetGreen()->PlayEffect(false);
	if(CPlayer::GetInstance()->GetRed()->GetPlay() == true)
		CPlayer::GetInstance()->GetRed()->PlayEffect(false);
	if(CPlayer::GetInstance()->GetYellow()->GetPlay() == true)
		CPlayer::GetInstance()->GetYellow()->PlayEffect(false);*/

}

void CFife::Update(float fElapsed)
{
	m_fCounter += fElapsed;
	if(m_fCounter >= .75f)
	{
		m_szNoteStack.clear();
		m_fCounter = 0;
	}
	if(Input())
		m_fCounter = 0;
	if(m_szNoteStack.size() > 2)
		m_szNoteStack.erase(m_szNoteStack.begin());
}